using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace Pix2Gallery2
{
    public partial class PSE6ToGalleryForm : Form
    {
        PSE6Interface EleInter = new PSE6Interface();
        //ElementsInterface EleInter = new ElementsInterface();
        public int numFailed;
        public DateTime StartOfRecentPeriod = DateTime.Today.AddDays(-7);
        private bool success;
        private Gallery2Interface G2int;
        private Pix2Gallery2Settings pix2gallery2Settings;
        public PSE6ToGalleryForm()
        {
            InitializeComponent();
            dateTimePickerFilterRecent.Value = StartOfRecentPeriod;
        }

        private void Elements2GalleryForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            EleInter.DisconnectFromPSEDB();
        }

        private void Elements2GalleryForm_Load(object sender, EventArgs e)
        {
            //This was only here to make testing easier
            //EleInter.ConnectToPSEDB(@"C:\Users\shanon\Pictures\Adobe Photoshop Album\Shanons.psa");
            //EleInter.PopulateCollectionTree(tvCollections);
            this.cmbPictureSize.SelectedIndex = 0;

            pix2gallery2Settings = new Pix2Gallery2Settings();
            if (this.pix2gallery2Settings.LoadAppSettings())
            {
                if ((pix2gallery2Settings.PSE_DefaultUploadPictureSize != "") && (pix2gallery2Settings.PSE_DefaultUploadPictureSize != null))
                    this.cmbPictureSize.SelectedIndex = Convert.ToInt16(pix2gallery2Settings.PSE_DefaultUploadPictureSize);
                else
                    this.cmbPictureSize.SelectedIndex = 0;
                this.ckbConvertToJPG.Checked = pix2gallery2Settings.PSE_DefaultConvertToJPG;
                if (!pix2gallery2Settings.PSE_HideInstructionsOnStartup)
                {
                    PSEUsageInstructionsForm usageForm = new PSEUsageInstructionsForm("6");
                    usageForm.ShowDialog();
                }
            }
            //20080926 wcf we must have a PSE database, so ask for it now
            OpenDatabase();
        }

        private void btnShowTreeDet_Click(object sender, EventArgs e)
        {
            TreeNode tn_Local;
            tn_Local = tvCollections.SelectedNode;
            MessageBox.Show("Name: " + tn_Local.Name +
                " Full Path: " + tn_Local.FullPath +
                " Handle: " + tn_Local.Handle +
                "Text: " + tn_Local.Text.ToString() +
                //"Caption: " + tn_Local
                EleInter.ReturnFullTreeIndex(tn_Local));
        }

        private void btnListPhotos_Click(object sender, EventArgs e)
        {
            ListPhotos();
        }
        private void ListPhotos()
        {
            TreeNode tn_Local;

            tn_Local = tvCollections.SelectedNode;
            //EleInter.PopulatePhotoList(lvPhoto);

            uint[] TempUint;
            TempUint = new uint[1];
            TempUint[0] = Convert.ToUInt32(tn_Local.Name);
            // wcf 20080919  EleInter.PopulatePhotoList(lvPhoto, TempUint);

        }

        private void treeViewCollections_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //e.Node.Name
            //TreeNode tn_Local;
            //tn_Local = treeViewCollections.SelectedNode;
            //EleInter.PopulatePhotoList(lvPhoto);
            uint[] TempUint;
            TempUint = new uint[1];
            TempUint[0] = Convert.ToUInt32(e.Node.Name);
            //EleInter.PopulatePhotoList(lvPhoto,TempUint);
            cbx0Stars.Checked = true;
            cbx1Stars.Checked = true;
            cbx2Stars.Checked = true;
            cbx3Stars.Checked = true;
            cbx4Stars.Checked = true;
            cbx5Stars.Checked = true;

            EleInter.PopulatePhotoListFromAlbumImageList(lvPhoto, TempUint);
            //20081009 check only "recent" pics
            FilterRecentPhotoSelection(StartOfRecentPeriod);

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenDatabase();
        }

        private void OpenDatabase()
        {
            try
            {
                //20080926 wcf Do we already have the PSE Db location?  If yes, go directly to it
                if (pix2gallery2Settings.DefaultPSEdb != null)
                {
                    // ofSelectDatabase.FileName = pix2gallery2Settings.DefaultPSEdb;
                    EleInter.ConnectToPSEDB(pix2gallery2Settings.DefaultPSEdb, G2int);
                }
                // Show the open file dialog box.
                else if (ofSelectDatabase.ShowDialog() == DialogResult.OK)
                {
                    // Load the Database file.
                    EleInter.ConnectToPSEDB(ofSelectDatabase.FileName, G2int);
                    //20080926 wcf probably not the best place but if we made it here, probably a good path so save it
                    pix2gallery2Settings.DefaultPSEdb = ofSelectDatabase.FileName;
                    pix2gallery2Settings.SaveAppSettings();
                }
                // Load up the list of Collections
                EleInter.PopulateCollectionTree(ref tvCollections);
               
                //Load up the list of photos

                EleInter.SetCollectionTreeIcons(tvCollections);
                //this.ListPhotos();


            }
            catch (System.OutOfMemoryException)
            {
                MessageBox.Show("For some reason the computer didnt like that file.",
                      "Invalid File", MessageBoxButtons.OK);
            }

        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EleInter.DisconnectFromPSEDB();
            tvCollections.Nodes.Clear();
            lvPhoto.Items.Clear();
        }

        private void lvPhoto_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvPhoto.SelectedItems.Count == 1)
            {
                ListViewItem objListItem;
                objListItem = lvPhoto.SelectedItems[0];
                string TempString = lvPhoto.SelectedItems[0].SubItems[2].Text.ToString();
                //string TempString = objListItem.ImageKey.ToString();
                try
                {
                    //We have to remove the last character

                    picPhotoPreview.Image = Image.FromFile(TempString);
                    //20081009 wcf Preview the Text fields also
                    //20081010 wcf reload the settings to get any changes
                    this.pix2gallery2Settings.LoadAppSettings();
                    string[] strTextfields = GetTextFields(Convert.ToInt32(lvPhoto.SelectedItems[0].Text));
                    txtbxTitle.Text = strTextfields[0];
                    txtbxSummary.Text = strTextfields[1];
                    txtbxDescription.Text = strTextfields[2];
                }
                catch
                {
                    MessageBox.Show("Error opening file " + TempString);

                }
                //picPhotoPreview.Image = Image.FromFile(lvPhoto.SelectedItems[0].SubItems[1].Text.ToString());
                //MessageBox.Show(lvPhoto.SelectedItems[0].SubItems[1].Text.ToString());
            }
        }

        private void btnUploadSelected_Click(object sender, EventArgs e)
        {
            if (this.lvPhoto.CheckedItems.Count == 0) return;
            this.numFailed = 0;
            int iTot = this.lvPhoto.CheckedItems.Count;
            this.Cursor = Cursors.WaitCursor;
            this.ThreadedUploadCheckedFiles();

            //Now update the Icons
            uint[] TempUint;
            TempUint = new uint[1];
            TempUint[0] = Convert.ToUInt32(tvCollections.SelectedNode.Name);
            //EleInter.PopulatePhotoList(lvPhoto,TempUint);
            //EleInter.PopulatePhotoListFromAlbumImageList(lvPhoto, TempUint);
            //EleInter.UpdateIconsAfterUpload(TempUint, tvCollections);
            //Update cosmetic stuff
            this.Cursor = Cursors.Default;
            this.toolStripProgressBarUpload.Visible = false;
            //Re-populate photo list
            EleInter.UpdateImageInfoAfterUploading();
            EleInter.PopulatePhotoListFromAlbumImageList(lvPhoto, TempUint);
            //20081009 check only "recent" pics
            FilterRecentPhotoSelection(StartOfRecentPeriod);
            EleInter.SetCollectionTreeIcons(tvCollections);
        }
        private int ThreadedUploadCheckedFiles()
        {
            //This function/method was taken from the main PIx2Gallery code

            int iCurrent = 1;
            int successfulUploads = 0;
            int failedUploads = 0;
            int skippedUploads = 0;
            int iTot = this.lvPhoto.CheckedItems.Count;

            this.toolStripProgressBarUpload.Value = 0;
            this.toolStripProgressBarUpload.Minimum = 0;
            this.toolStripProgressBarUpload.Maximum = iTot;
            this.toolStripProgressBarUpload.Step = 1;
            this.toolStripProgressBarUpload.Visible = true;
            string album = EleInter.ReturnAlbum(tvCollections, G2int);
            // >> 20070912 SM
            //bool requireCaption = false;
            //bool requirePeopleTags = false;
            //bool requireAllTags = false;
            //int descriptionPopulatedBy = 0; // 0 = nothing, 1 = caption, 2 = people tags, 3 = all tags
            //int titlePopulatedBy = 1;
            //int summaryPopulatedBy = 0;
            pix2gallery2Settings = new Pix2Gallery2Settings();

            this.pix2gallery2Settings.LoadAppSettings();
            //{

            //    if ((pix2gallery2Settings.PSE_DescriptionPopulatedBy == null) || (pix2gallery2Settings.PSE_DescriptionPopulatedBy == ""))
            //        descriptionPopulatedBy = 0;
            //    else
            //        descriptionPopulatedBy = Convert.ToInt16(pix2gallery2Settings.PSE_DescriptionPopulatedBy);
            //    if ((pix2gallery2Settings.PSE_SummaryPopulatedBy == null) || (pix2gallery2Settings.PSE_SummaryPopulatedBy == ""))
            //        summaryPopulatedBy = 0;
            //    else
            //        summaryPopulatedBy = Convert.ToInt16(pix2gallery2Settings.PSE_SummaryPopulatedBy);
            //    if ((pix2gallery2Settings.PSE_TitlePopulatedBy == null) || (pix2gallery2Settings.PSE_TitlePopulatedBy == ""))
            //        titlePopulatedBy = 1;
            //    else
            //        titlePopulatedBy = Convert.ToInt16(pix2gallery2Settings.PSE_TitlePopulatedBy);
            //}
            //if ((descriptionPopulatedBy == 1) || (titlePopulatedBy == 1) || (summaryPopulatedBy == 1))
            //    requireCaption = true;
            //if ((descriptionPopulatedBy == 2) || (titlePopulatedBy == 2) || (summaryPopulatedBy == 2))
            //    requirePeopleTags = true;
            //if ((descriptionPopulatedBy == 3) || (titlePopulatedBy == 3) || (summaryPopulatedBy == 3))
            //    requireAllTags = true;
            //// << 20070912 SM

            foreach (ListViewItem itemChecked in this.lvPhoto.CheckedItems)
            {
                //20080926 wcf
                string[] strTextfields = { "", "", "" };
                // If this image has already been uploaded, skip it.
                if (itemChecked.ImageIndex == 3) //Green - already exists.
                {
                    skippedUploads++;
                    continue;
                }

                this.toolStripProgressBarUpload.PerformStep();
                Gallery2Interface G2intLoc = this.G2int;
                //string album = this.treeViewAlbums.SelectedNode.Name; //I'm going to have to get this from somewhere else.
                //string album = EleInter.ReturnAlbum(tvCollections,G2intLoc);
                //album = "19"; //TEST VALUE - Must populate this correctly later
                //20080926 wcf So we may have selected options- check that first
                int intItemId = Convert.ToInt32(itemChecked.Text);
                if (this.pix2gallery2Settings.PseField2Gallery2Fields.Count > 0)
                {
                    //20080926 wcf yes, we have stored options.  Currently all fields are allowed the same options
                    strTextfields = GetTextFields(intItemId);
                }
                string picTitle = strTextfields[0];
                string picSummary = strTextfields[1];
                string picDescription = strTextfields[2];

                //>> 20070912 SM
                //string caption = "";
                //string peopleTags = "";
                //string allTags = "";
                //if (requireCaption == true)
                //    caption = itemChecked.SubItems[3].Text.ToString();
                //if (requirePeopleTags == true)
                //    peopleTags = EleInter.ReturnTagsForPicture(Convert.ToUInt16(itemChecked.Text), true, false, false, false);
                //if (requireAllTags == true)
                //    allTags = EleInter.ReturnTagsForPicture(Convert.ToUInt16(itemChecked.Text), true, true, true, true);
                //switch (descriptionPopulatedBy)
                //{
                //    case 1:
                //        picDescription = caption;
                //        break;
                //    case 2:
                //        picDescription = peopleTags;
                //        break;
                //    case 3:
                //        picDescription = allTags;
                //        break;
                //}
                //switch (summaryPopulatedBy)
                //{
                //    case 1:
                //        picSummary = caption;
                //        break;
                //    case 2:
                //        picSummary = peopleTags;
                //        break;
                //    case 3:
                //        picSummary = allTags;
                //        break;
                //}
                //switch (titlePopulatedBy)
                //{
                //    case 1:
                //        picTitle = caption;
                //        break;
                //    case 2:
                //        picTitle = peopleTags;
                //        break;
                //    case 3:
                //        picTitle = allTags;
                //        break;
                //}
                //<< 20070912 SM
                //string filePath = itemChecked.ImageKey;
                string filePath = itemChecked.SubItems[2].Text.ToString();

                //We need to construct a filename. This will be the PSE image ID, with the file extension.
                //Like 342.jpg or 246.gif.
                //Firstly, lets get the file extension of the file we are uploading.
                int LastDotPos = filePath.LastIndexOf(".");
                string fileName = filePath.Substring(LastDotPos, filePath.Length - LastDotPos);
                fileName = itemChecked.Text + fileName;
                string newFilePath;

                //20070904 >> SM
                //>> 20070911 SM
                //if (this.cmbPictureSize.SelectedIndex > 0)
                if ((this.cmbPictureSize.SelectedIndex > 0) || (this.ckbConvertToJPG.Checked == true))
                //<< 20070911 SM
                {
                    //Resize the picture here and reset the filePath variable...
                    newFilePath = this.ResizePicture(filePath, this.cmbPictureSize.SelectedIndex);
                    filePath = newFilePath;
                }
                //20070904 << SM
                this.SetSuccess(true);
                UploadFileThread upThread =
                    new UploadFileThread(album, picTitle, filePath, fileName, picSummary, picDescription, G2intLoc, new SetSuccessCallback(this.SetSuccess));
                Thread t = new Thread(new ThreadStart(upThread.ThreadProc));
                t.Start();
                this.toolStripStatusLabel.Text = "Uploading file " + Path.GetFileName(filePath) + " ";
                t.Join();

                t = null;

                if (this.success)
                {
                    successfulUploads += 1;
                    itemChecked.Checked = false;
                    this.toolStripStatusLabel.Text = "Failed uploading " + Path.GetFileName(filePath) + " ";
                }
                else
                {
                    failedUploads += 1;
                }

                iCurrent += 1;
            }
            /*
            if (successfulUploads == 1 && failedUploads == 1)
            {
                this.toolStripStatusLabel.Text = successfulUploads + " successful upload and " + failedUploads + " failed upload ";
            }
            else if (successfulUploads != 1 && failedUploads == 1)
            {
                this.toolStripStatusLabel.Text = successfulUploads + " successful uploads and " + failedUploads + " failed upload ";
            }
            else
            {
                this.toolStripStatusLabel.Text = successfulUploads + " successful uploads and " + failedUploads + " failed uploads ";
            }
            */
            this.toolStripStatusLabel.Text = "Successful uploads: " + successfulUploads + " ; Failed uploads: " + failedUploads +
                " ; Skipped uploads: " + skippedUploads;
            this.statusStrip.Refresh();

            return failedUploads;
        }
        private string[] GetTextFields(int intPictureId)
        {
            //20081009 wcf clean this up!
            string[] results = { "", "", "" };
            string[] strFields = { "", "", "", "", "", "" };
            if (this.pix2gallery2Settings.PseField2Gallery2Fields.Count > 0)
            {
                //20080926 wcf yes, we have stored options.  Currently all fields are allowed the same options
                strFields = EleInter.GetPSE2G2Fields(intPictureId, pix2gallery2Settings);
            }
            char[] MyChar = { ' ', ',' };
            for (int i = 0; i <= 5; i++)
            {
                strFields[i] = strFields[i].TrimEnd(MyChar);
            }
            results[0] = this.pix2gallery2Settings.UseLongTitles == false ? strFields[0] : strFields[3];
            results[1] = this.pix2gallery2Settings.UseLongSummaries == false ? strFields[1] : strFields[4];
            results[2] = this.pix2gallery2Settings.UseLongDescriptions == false ? strFields[2] : strFields[5];
            return results;
        }
        private void SetSuccess(bool success)
        {
            this.success = success;
        }
        public void SetG2Int(Gallery2Interface NewG2int)
        {
            G2int = NewG2int;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ListViewItem objListItem;
            objListItem = lvPhoto.SelectedItems[0];
            //MessageBox("Selected photo text: " lvPhoto.SelectedItems.in
            Debug.WriteLine("SubItem 0: " + objListItem.SubItems[0]);
            Debug.WriteLine("SubItem 1: " + objListItem.SubItems[1]);
            Debug.WriteLine("SubItem 2: " + objListItem.SubItems[2]);
            Debug.WriteLine("SubItem 3: " + objListItem.SubItems[3].ToString());
            Debug.WriteLine("SubItem 3: " + objListItem.SubItems[3].Text.ToString());
            Debug.WriteLine("ImageKey: " + objListItem.ImageKey.ToString());
        }
        public delegate void SetSuccessCallback(bool failed);

        public class UploadFileThread
        {
            // State information used in the task.
            private string album;
            private string caption;
            private string filePath;
            private string fileName;
            // >> 20070912 SM
            private string picDescription;
            private string picSummary;
            // << 20070912 SM
            private Gallery2Interface G2int;
            private SetSuccessCallback uploadCallback;

            // The constructor obtains the state information.
            public UploadFileThread(string album, string caption, string filePath, string fileName,
                string picDescription, string picSummary, Gallery2Interface G2int, SetSuccessCallback uploadCallback)
            {
                this.album = album;
                this.caption = caption;
                this.filePath = filePath;
                this.fileName = fileName;
                this.G2int = G2int;
                this.uploadCallback = uploadCallback;
                // >> 20070912 SM
                this.picSummary = picSummary;
                this.picDescription = picDescription;
                // << 20070912 SM
            }

            public void ThreadProc()
            {
                bool res = this.G2int.UploadFileToAlbumEvents(this.album, this.caption, this.filePath, this.fileName, this.picSummary, this.picDescription, false);
                if (uploadCallback != null)
                    uploadCallback(res);
            }
        }


        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBoxElements2Pix2Gallery aboutBox = new AboutBoxElements2Pix2Gallery();
            aboutBox.ShowDialog();
        }


        private void labelChoosePhotos_Click(object sender, EventArgs e)
        {

        }
        public void InvertPhotoSelection()
        {
            int PhotoCount = lvPhoto.Items.Count;
            for (int i = 0; i < PhotoCount; i++)
            {
                if (lvPhoto.Items[i].ImageIndex != 3) // 20081008 wcf can't upload it again!
                {
                    lvPhoto.Items[i].Checked = !lvPhoto.Items[i].Checked;
                }
            }

        }

        private void btnInvertSelection_Click(object sender, EventArgs e)
        {
            InvertPhotoSelection();
        }
        public void UpdateStarSelection()
        {
            int PhotoCount = lvPhoto.Items.Count;
            for (int i = 0; i < PhotoCount; i++)
            {
                string blah1 = lvPhoto.Items[i].SubItems[0].Text.ToString();
                string blah2 = lvPhoto.Items[i].SubItems[1].Text.ToString();
                string blah3 = lvPhoto.Items[i].SubItems[2].ToString();
                string blah4 = lvPhoto.Items[i].SubItems[3].ToString();
                string blah5 = lvPhoto.Items[i].SubItems[4].ToString();
                int Stars = Convert.ToInt16(lvPhoto.Items[i].SubItems[4].Text.ToString());
                switch (Stars)
                {
                    case 0:
                        lvPhoto.Items[i].Checked = cbx0Stars.Checked;
                        break;
                    case 1:
                        lvPhoto.Items[i].Checked = cbx1Stars.Checked;
                        break;
                    case 2:
                        lvPhoto.Items[i].Checked = cbx2Stars.Checked;
                        break;
                    case 3:
                        lvPhoto.Items[i].Checked = cbx3Stars.Checked;
                        break;
                    case 4:
                        lvPhoto.Items[i].Checked = cbx4Stars.Checked;
                        break;
                    case 5:
                        lvPhoto.Items[i].Checked = cbx5Stars.Checked;
                        break;
                }
            }
        }

        private void cbx0Stars_CheckedChanged(object sender, EventArgs e)
        {
            UpdateStarSelection();
        }

        private void cbx1Stars_CheckedChanged(object sender, EventArgs e)
        {
            UpdateStarSelection();
        }

        private void cbx2Stars_CheckedChanged(object sender, EventArgs e)
        {
            UpdateStarSelection();
        }

        private void cbx3Stars_CheckedChanged(object sender, EventArgs e)
        {
            UpdateStarSelection();
        }

        private void cbx4Stars_CheckedChanged(object sender, EventArgs e)
        {
            UpdateStarSelection();
        }

        private void cbx5Stars_CheckedChanged(object sender, EventArgs e)
        {
            UpdateStarSelection();
        }
        private string ResizePicture(string filePath, int sizeIndex)
        {
            // Copy of same function from UploadForm. I would have prefered to call that, but
            // it was made private, so I cant.
            //>> 20070911 SM
            //if (sizeIndex == 0) return filePath;
            if ((sizeIndex == 0) && (this.ckbConvertToJPG.Checked == false)) return filePath;
            //<< 20070911 SM

            int maxWidth;
            int maxHeight;
            string newPath;

            switch (sizeIndex)
            {
                case 1:
                    maxWidth = 1280;
                    maxHeight = 1024;
                    break;
                case 2:
                    maxWidth = 1024;
                    maxHeight = 768;
                    break;
                case 3:
                    maxWidth = 800;
                    maxHeight = 600;
                    break;
                default:
                    maxWidth = 640;
                    maxHeight = 480;
                    break;
            }
            //>> 20070911 SM
            if ((sizeIndex == 0) && (this.ckbConvertToJPG.Checked == true))
            {
                maxWidth = 0;
                maxHeight = 0;
            }
            //<< 20070911 SM

            //ImageHandler imgHandler = new ImageHandler();

            ////newPath = imgHandler.ResizePicture_ConstantAspectRatio(filePath, maxWidth, maxHeight);
            //newPath = imgHandler.ResizePicture_ConstantAspectRatio(filePath, maxWidth, maxHeight);

            //if (imgHandler.resizeError)
            //{
            //    MessageBox.Show("Resize Picture Error - See Log File in " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
            //    imgHandler = null;
            //    return filePath;
            //}

            //imgHandler = null;

            newPath = ImageHandler.ResizePicture_ConstantAspectRatio(filePath, maxWidth, maxHeight);
            
            return newPath;
        } //ResizePicture
        private void UpdateConvertToJPGEditable()
        {
            //Function will update the Convert To JPG field to editable (or not)
            if (cmbPictureSize.SelectedIndex == 0)
            {
                ckbConvertToJPG.Enabled = true;
            }
            else
            {
                ckbConvertToJPG.Checked = false;
            }
        }

        private void cmbPictureSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateConvertToJPGEditable();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PSEOptionsForm pseOptionsForm = new PSEOptionsForm();
            pseOptionsForm.Show();
        }
        //20080923 wcf Not needed- handled in select
        //private void button1_Click_1(object sender, EventArgs e)
        //{
        //    int rating = EleInter.GetRating(482);
        //}

        //private void button1_Click_2(object sender, EventArgs e)
        //{
        //    int rating = EleInter.GetRating(482);
        //}

        private void usageInstructionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PSEUsageInstructionsForm usageForm = new PSEUsageInstructionsForm("6");
            usageForm.ShowDialog();
        }

        private void dateTimePickerFilterRecent_CloseUp(object sender, EventArgs e)
        {
            StartOfRecentPeriod = dateTimePickerFilterRecent.Value;
            //wcf 20080922 Filter Pics by date in the selected album
            FilterRecentPhotoSelection(StartOfRecentPeriod);
        }
        public void FilterRecentPhotoSelection(DateTime StartOfRecentPeriod)
        {
            int PhotoCount = lvPhoto.Items.Count;
            for (int i = 0; i < PhotoCount; i++)
            {
                if (Convert.ToDateTime(lvPhoto.Items[i].SubItems[5].Text) < StartOfRecentPeriod)
                {
                    lvPhoto.Items[i].Checked = false;
                }
                else
                {
                    lvPhoto.Items[i].Checked = true;
                }
            }

        }



        private void lvPhoto_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            //prohibit selecting an item that is already uploaded
            if (lvPhoto.Items[e.Index].ImageIndex == 3)
            {
                System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
                messageBoxCS.AppendFormat("{0} = {1}", "Photo ", lvPhoto.Items[e.Index].SubItems[1].Text);
                messageBoxCS.AppendLine();
                MessageBox.Show(messageBoxCS.ToString(), "Already Uploaded!");
                e.NewValue = e.CurrentValue;
            }

        }
    }
} //namespace
